<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个税计算封装为函数</title>
</head>
<body>
    <script>
        
        function calcTax(salary){
            var baseSalary = 0;
            if(salary > 7662){
                baseSalary = 7662;
            }
            else{
                baseSalary = salary;
            }
            //计算三险一金
            var sanXianYiJin = baseSalary * (0.08 + 0.02 + 0.005 + 0.12);

            //3.应纳税所得额
            var yingNaShuiSuoDeE = salary - sanXianYiJin - 3500;

            //确定税率和速算扣除数
            var shuiLv = 0;
            var suSuanKouChuShu = 0;
            var tax = 0;
            if(yingNaShuiSuoDeE <= 0){
                //不交税
                tax = 0;
            }
            else{
                //不交税
               if(yingNaShuiSuoDeE <= 1500){
                    shuiLv = 0.03;
                    suSuanKouChuShu = 0;
                }
                else if(yingNaShuiSuoDeE <= 4500){
                    shuiLv = 0.1;
                    suSuanKouChuShu = 105;
                }
                else if(yingNaShuiSuoDeE <= 9000){
                    shuiLv = 0.2;
                    suSuanKouChuShu = 555;
                }
                else if(yingNaShuiSuoDeE <= 35000){
                    shuiLv = 0.25;
                    suSuanKouChuShu = 1005;
                }
                else if(yingNaShuiSuoDeE <= 55000){
                    shuiLv = 0.3;
                    suSuanKouChuShu = 2755;
                }
                else if(yingNaShuiSuoDeE <= 80000){
                    shuiLv = 0.35;
                    suSuanKouChuShu = 5505;
                }
                else{
                    shuiLv = 0.45;
                    suSuanKouChuShu = 13505;
                }

                tax = yingNaShuiSuoDeE * shuiLv - suSuanKouChuShu;
            }

            return tax;
            
        }

        var geShui = calcTax(7000);
        console.log(geShui);
        calcTax(8000)

        calcTax(10000)

    </script>
</body>
</html>